/*
 * H265PropParser.h
 *
 *  Created on: 2016年4月7日
 *      Author: terry
 */

#ifndef H265PROPPARSER_H_
#define H265PROPPARSER_H_

#include "BasicType.h"
#include <string>


namespace av
{

enum H265NalUnitType
{
	NAL_TRAIL_N = 0,
	NAL_TRAIL_R = 1,
	NAL_TSA_N = 2,
	NAL_TSA_R = 3,
	NAL_STSA_N = 4,
	NAL_STSA_R = 5,
	NAL_RADL_N = 6,
	NAL_RADL_R = 7,
	NAL_RASL_N = 8,
	NAL_RASL_R = 9,
	NAL_BLA_W_LP = 16,
	NAL_BLA_W_RADL = 17,
	NAL_BLA_N_LP = 18,
	NAL_IDR_W_RADL = 19,
	NAL_IDR_N_LP = 20,
	NAL_CRA_NUT = 21,

    NAL_UNIT_VPS = 32,
    NAL_UNIT_SPS = 33,
    NAL_UNIT_PPS = 34,
    NAL_UNIT_ACCESS_UNIT_DELIMITER = 35,
    NAL_UNIT_EOS = 36,
    NAL_UNIT_EOB = 37,
    NAL_UNIT_FILLER_DATA = 38,
    NAL_UNIT_PREFIX_SEI = 39,
    NAL_UNIT_SUFFIX_SEI = 40,
};


class H265PropParser
{
public:
	class NaluPacket
	{
	public:
		const uint8_t*    data;
		int length;
		uint8_t type;
		int prefix;

		NaluPacket() :
			data(),
			length(),
			type(),
			prefix()
		{
		}

		~NaluPacket()
		{
		}
	};

public:
	H265PropParser();
	virtual ~H265PropParser();

	static const size_t START_CODE_LENGTH = 4;
	static const uint8_t s_startCode[START_CODE_LENGTH];

	static const uint8_t* getStartCode();

	static bool startWithCode(const uint8_t* data, size_t length);
	static size_t findStartCode(const uint8_t* data, size_t length, size_t start);

	static bool splitPropSet(const uint8_t* data, size_t length,
					std::string& vps, std::string& sps, std::string& pps);

	static bool parseNalu(const uint8_t* data, size_t length, NaluPacket& pkt);

	static bool isKeyFrame(uint8_t naluType);

};


} /* namespace av */

#endif /* H265PROPPARSER_H_ */
